function usage () {
   more << EOF 
     -h|--help  help info
     -t         hdd power on hour limit
     example
     ************************************
     $0 
     $0 -t 800
     ************************************
EOF
}

function logs () {
if [[ "`ls logs`" != ""  ]]
then
   read -p "Please check if romove logs file: (Y/n)" r
   case $r in
     Y|y)
       rm -rf ./logs/*
       ;;
     N|n)
       mkdir -p ./logs/before
       mv -f ./logs/*.log ./logs/before
    esac
fi
}

function SATA_hdd () {
fdisk -l|grep -io 'disk /dev/sd..'|tr -s ":" " "|awk '{print $2}'>./logs/SATA_tmp.log
sleep 0.5
cat ./logs/SATA_tmp.log|sort|awk '{print length,$0}'|sort -n| awk '{print $2}' >./logs/SATA.log
SATA=`cat ./logs/SATA.log`
if [[ "$SATA" !=  "" ]]
then
    for hd in $SATA
	do
	  HDD_H=`smartctl -A $hd|grep -i 'power_on_hours'|awk '{print $NF}'`
	  echo  "$hd      Power_on_Hours ：$HDD_H(h)" >> ./logs/Hdd_Power_on_Hours.log
	  if [[ $replace_hdd -eq 1 ]]
	  then 
	   replace_hd
	  fi
	done
else
     echo -e "\033[31m SUT not install SATA HDD!! \033[0m"
fi
rm -rf ./logs/SATA_tmp.log
}

function install_nvme_tool () {
nvme_cmd=`command -v nvme_smart`
if [[ "$nvme_cmd" ==  '' ]]
then 
   cd tool 
   rpm -ivh --nodeps --force *.rpm
   cd ..
fi  
}

function NVME_hdd () {
install_nvme_tool
fdisk -l|grep -io 'disk /dev/nvme....'|tr -s ":" " "|awk '{print $2}'  >./logs/nvme_tmp.log
sleep 0.5
cat ./logs/nvme_tmp.log|sort|awk '{print length,$0}'|sort -n| awk '{print $2}' >./logs/nvme.log
NVME=`cat ./logs/nvme.log`
if [[ "$NVME" !=  "" ]]
then
    for hd in $NVME
	do
	  HDD_H=`nvme_smart $hd|grep -i 'power_on_hours'|awk '{print $NF}'|sed s/,//g`
	  echo  "$hd  Power_on_Hours ：$HDD_H(h)" >> ./logs/Hdd_Power_on_Hours.log
	  if [[ $replace_hdd -eq 1 ]]
	  then 
	   replace_hd
	  fi
	done
else
     echo -e "\033[31m SUT not install NVME HDD!! \033[0m"
fi
rm -rf ./logs/nvme_tmp.log
}

function replace_hd () { 
if [[ $HDD_H -gt $time_limit ]]
    then       
	    echo  "$hd  Power_on_Hours > $time_limit(h), Please replace this hdd" | tee -a ./logs/Need_replace_hdd.log
fi
}

replace_hdd=0
while [[ $1 != "" ]]
do
   case $1 in 
   -h|--help)
       usage
       exit
       ;;
   -t)
       replace_hdd=1
       shift
       time_limit=$1
       ;;
    *)
      usage
      esac
    shift
done
	  

logs
SATA_hdd
NVME_hdd

